<script>
import { loadAdminTeacher, removeAdminTeacher } from '@/api/admin/teacher'
import { ElNotification } from 'element-plus'

export default {
  data() {
    return {
      total: 0,
      params: {
        pageNo: 1,
        pageSize: 10,
        tname: '',
        department: '',
      },
      list: [],
    }
  },
  methods: {
    async initTeacherData() {
      const { total, list } = await loadAdminTeacher(this.params)
      this.total = total
      this.list = list
    },
    async deleteTeacher(row) {
      await removeAdminTeacher(row.teacher_id)
      ElNotification.success({
        title: `“${row.teacher_name}”删除成功`,
      })
      this.initTeacherData()
    },
  },
  created() {
    this.initTeacherData()
  },
}
</script>

<template>
  <el-card>
    <template #header>教师管理</template>
    <div class="nav-bar">
      <div class="search">
        <el-input size="small" placeholder="系名" clearable v-model="params.department"></el-input>
        <el-input size="small" placeholder="教师名" clearable v-model="params.tname"></el-input>
        <el-button size="small" type="primary" @click="initTeacherData">
          <template #icon>
            <el-icon><EpSearch /></el-icon>
          </template>
          <span>查询</span>
        </el-button>
      </div>
      <div>
        <el-button size="small" type="success">
          <template #icon>
            <el-icon><EpPlus /></el-icon>
          </template>
          <span>添加</span>
        </el-button>
      </div>
    </div>
    <el-table border stripe size="small" :data="list">
      <template #empty>
        <el-empty description="暂无数据" />
      </template>
      <el-table-column label="序号" type="index" width="60"></el-table-column>
      <el-table-column label="教师工号(手机号)" prop="teacher_number"></el-table-column>
      <el-table-column label="教师姓名" prop="teacher_name"></el-table-column>
      <el-table-column label="所属系" prop="department_name"></el-table-column>
      <el-table-column label="操作" width="120" #default="{ row }">
        <el-button type="primary" link>编辑</el-button>
        <el-popconfirm :title="`是否删除教师“${row.teacher_name}”？`" @confirm="deleteTeacher(row)">
          <template #reference>
            <el-button type="danger" link>删除</el-button>
          </template>
        </el-popconfirm>
      </el-table-column>
    </el-table>
    <el-pagination
      size="small"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[5, 10, 20, 30, 40, 50]"
      :total="total"
      v-model:page-size="params.pageSize"
      v-model:current-page="params.pageNo"
      @change="initTeacherData"
    />
  </el-card>
</template>

<style scoped>
.nav-bar {
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
  margin-bottom: 14px;
}
.nav-bar .el-input {
  width: 180px;
  margin-right: 10px;
}
.el-pagination {
  margin-top: 14px;
  justify-content: center;
}
</style>
